<template>
  <div>
    <!-- 查询条件 -->
    <!-- <table>
      <tr>
        <td>流水号:</td>
        <td>
          <el-input v-model="serialNumber" placeholder="请输入" style="width: 180px;"></el-input>
        </td>
        <td>名称:</td>
        <td>
          <el-input v-model="name" placeholder="请输入名称" style="width: 180px;"></el-input>
        </td>
        <td>收款方:</td>
        <td>
          <el-input v-model="payee" placeholder="请输入收款方" style="width: 180px;"></el-input>
        </td>
        <td>支付类别:</td>
        <td>
          <el-select v-model="typeId" clearable>
            <el-option v-for="item in TypeList" :key="item.id" :label="item.typeName" :value="item.id">
            </el-option>
          </el-select>
        </td>
      </tr>
      <tr>
        <td>状态:</td>
        <td>
          <el-select v-model="state" clearable>
            <el-option label="待审核" value="1"></el-option>
            <el-option label="审核通过" value="2"></el-option>
            <el-option label="审核失败" value="3"></el-option>
            <el-option label="部分付款" value="4"></el-option>
            <el-option label="全部付款" value="5"></el-option>
          </el-select>
        </td>
        <td>起始时间:</td>
        <td>
          <el-date-picker v-model="start" type="date" placeholder="选择开始日期时间">
          </el-date-picker>

          <el-date-picker v-model="end" type="date" placeholder="选择结束日期时间">
          </el-date-picker>
        </td>
        <td style="width: 120px">
          <el-button type="primary" @click="onPaymentList()">查询</el-button>
        </td>
        <td>
          <el-button type="success" @click="onAdd()">添加</el-button>
        </td>
        <td>
          <el-button type="danger" @click="onBathdel()">批量删除</el-button>
        </td>
      </tr>
    </table> -->

    <div style="display: flex;justify-content: flex-start; align-items: center" class="demo-form-inline">
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item label="流水号:">
          <el-input v-model="serialNumber" placeholder="请输入流水号" style="width: 120px"></el-input>
        </el-form-item>
        <el-form-item label="名称:">
          <el-input v-model="name" placeholder="请输入名称" style="width: 120px"></el-input>
        </el-form-item>
        <el-form-item label="收款方:">
          <el-input v-model="payee" placeholder="请输入收款方" style="width: 120px;"></el-input>
        </el-form-item>
        <el-form-item label="支付类别:">
          <el-select v-model="typeId" placeholder="请选择" style="width: 90px">
            <el-option label="请选择">请选择 </el-option>
            <el-option v-for="item in TypeList" :key="item.id" :label="item.typeName" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="起始时间:">
          <el-date-picker type="date" placeholder="选择日期" v-model="start" clearable
            style="width:120px"></el-date-picker>-<el-date-picker type="date" placeholder="选择日期" v-model="end" clearable
            style="width:120px"></el-date-picker>
        </el-form-item>
        <!-- <el-form-item label="状态:" style="width: 120px">
          <el-select v-model="state" clearable>
            <el-option label="待审核" value="1"></el-option>
            <el-option label="审核通过" value="2"></el-option>
            <el-option label="审核失败" value="3"></el-option>
            <el-option label="部分付款" value="4"></el-option>
            <el-option label="全部付款" value="5"></el-option>
          </el-select>
        </el-form-item> -->
        <el-button type="primary" @click="onPaymentList()">查询</el-button>
        <el-button type="success" @click="onAdd()">添加</el-button>
        <el-button type="danger" @click="onBathdel()">批量删除</el-button>
      </el-form>
    </div>

    <!-- 应付列表 -->
    <el-table :data="tableData" border style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column prop="serialNumber" label="流水号" align="center">
      </el-table-column>
      <el-table-column prop="name" label="名称" align="center">
      </el-table-column>
      <el-table-column prop="typeName" label="财务类别" align="center">
      </el-table-column>
      <el-table-column prop="payer" label="收款方" align="center">
      </el-table-column>
      <el-table-column prop="payable" label="应付金额" align="center">
      </el-table-column>
      <el-table-column prop="netReceipts" label="实收金额" align="center">
      </el-table-column>
      <el-table-column prop="state" label="审核状态" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.state == 1">待审核</span>
          <span v-if="scope.row.state == 2">审核通过</span>
          <span v-if="scope.row.state == 3">审核失败</span>
          <span v-if="scope.row.state == 4">部分付款</span>
          <span v-if="scope.row.state == 5">全部付款</span>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" align="center">

        <template slot-scope="scope">
          {{ scope.row.createTime.substring(0, 10) }}
        </template>
      </el-table-column>
      <el-table-column prop="address" label="操作" width="280px">

        <template slot-scope="scope">
          <el-link type="primary" class="el-icon-view" @click="onDetails(scope.row.id)" style="width: 60px">查看</el-link>
          <el-link type="success" class="el-icon-edit" style="width: 60px" @click="onjump(scope.row.id)">编辑</el-link>
          <!-- <el-link type="warning" class="el-icon-coin" style="width: 60px"
            >收款</el-link
          > -->
          <el-link type="danger" class="el-icon-delete" style="width: 60px" @click="onDelete(scope.row.id)">删除</el-link>
          <el-link type="info" class="el-icon-user" @click="onUpdState(scope.row.id)">审核</el-link>
        </template>
      </el-table-column>
    </el-table>

    <br>

    <!-- 分页 -->
    <div>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageIndex"
        :page-sizes="[5, 10, 15, 20]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
        :total="totalCount">
      </el-pagination>
    </div>

    <!-- 弹出层 -->
    <el-dialog title="新增应付" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px" class="demo-form">
        <el-row :gutter="24">
          <el-col :span="12" :offset="0">
            <el-form-item label="流水号" prop="serialNumber">
              <el-input v-model="form.serialNumber" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="类别" prop="typeId">
              <el-select v-model="form.typeId" placeholder="请选择">
                <el-option v-for="item in TypeList" :key="item.id" :label="item.typeName" :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="付款方" prop="payer">
              <el-input v-model="form.payer"></el-input>
            </el-form-item>
            <el-form-item label="应付金额" prop="payable">
              <el-input v-model="form.payable"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" :offset="0">
            <el-form-item label="名称" prop="name">
              <el-input v-model="form.name"></el-input>
            </el-form-item>
            <el-form-item label="付款日" prop="lastPaymentDate">
              <el-date-picker v-model="form.lastPaymentDate" type="datetime" placeholder="选择日期时间">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="收款方" prop="payee">
              <el-input v-model="form.payee"></el-input>
            </el-form-item>
            <el-form-item label="备注" prop="remarks">
              <el-input type="textarea" :rows="2" placeholder="请输入内容" v-model="form.remarks">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item>
          <el-button type="primary" @click="submitForm('form')">添加</el-button>
          <el-button @click="resetForm('form')">返回</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!-- 查看 -->
    <el-dialog title="查看应付" :visible.sync="dialogForms">
      <el-form :model="forms" :rules="rules" ref="forms" label-width="100px" class="demo-forms">
        <el-row :gutter="24">
          <el-col :span="12" :offset="0">
            <el-form-item label="流水号" prop="serialNumber">
              <el-input v-model="forms.serialNumber" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="类别" prop="typeId">
              <el-select v-model="forms.typeId" placeholder="请选择">
                <el-option v-for="item in TypeList" :key="item.id" :label="item.typeName" :value="item.id"
                  :disabled="true">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="应付金额" prop="payable">
              <el-input v-model="forms.payable" :disabled="true"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" :offset="0">
            <el-form-item label="名称" prop="name">
              <el-input v-model="forms.name" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="付款日" prop="lastPaymentDate">
              <el-date-picker v-model="forms.lastPaymentDate" type="datetime" placeholder="选择日期时间" :disabled="true">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="收款方" prop="payee">
              <el-input v-model="forms.payee" :disabled="true"></el-input>
            </el-form-item>
          </el-col>
          <el-form-item label="备注" prop="remarks">
            <el-input type="textarea" :rows="2" placeholder="请输入内容" v-model="forms.remarks" :disabled="true">
            </el-input>
          </el-form-item>
        </el-row>

        <el-form-item>
          <el-button type="primary" @click="CheckView('forms')">取消</el-button>
          <el-button @click="returnForms('forms')">返回</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { autoGenerateCode } from "@/utils/utils";
import {
  ShowPaymentapi,
  GetPaytypeapi,
  AddPaymentapi,
  DeletePaymentapi,
  BatchDelPaymentapi,
  ReversePaymentapi,
  UpdateStateapi,
} from "@/api/FinanceManage/Payments";
export default {
  data() {
    return {
      //新增应付
      form: {
        serialNumber: "",
      },
      //查看
      forms: {},
      //验证
      rules: {
        serialNumber: [
          { required: true, message: "请输入流水号", trigger: "blur" },
        ],
        name: [
          { required: true, message: "请输入厂家编码编号", trigger: "blur" },
        ],
        lastPaymentDate: [
          {
            type: "date",
            required: true,
            message: "请选择时间",
            trigger: "change",
          },
        ],
        payer: [{ required: true, message: "请输入付款方", trigger: "blur" }],
        payable: [
          { required: true, message: "请输入应收金额", trigger: "blur" },
        ],
        payee: [{ required: true, message: "请输入收款方", trigger: "blur" }],
        typeId: [
          { required: true, message: "请选择支付类别", trigger: "change" },
        ],
        remarks: [{ required: true, message: "请填写备注", trigger: "blur" }],
      },
      dialogFormVisible: false,
      tableData: [], //应付列表】
      multipleSelection: [], //复选框
      serialNumber: "",
      name: "",
      typeId: "",
      state: "",
      payee: "",
      start: "",
      end: "",
      pageIndex: 1,
      pageSize: 5,
      totalCount: 0,
      pageCount: 0,
      TypeList: [], //支付类别
      dialogForms: false,
    };
  },
  methods: {
    //加载应付列表
    onPaymentList() {
      var dto = {
        serialNumber: this.serialNumber,
        name: this.name,
        payee: this.payee,
        typeId: this.typeId || 0,
        state: this.state || 0,
        start: this.start || null,
        end: this.end || null,
        pageIndex: this.pageIndex,
        pageSize: this.pageSize,
      };
      ShowPaymentapi(dto).then((res) => {
        this.tableData = res.data.data;
        this.totalCount = res.data.totalCount;
        this.pageCount = res.daata.pageCount;
      });
    },

    //加载支付类别
    onTypeList() {
      GetPaytypeapi().then((res) => {
        this.TypeList = res.data.data;
      });
    },

    //复选框
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },

    //分页
    handleSizeChange(val) {
      this.pageSize = val;
      this.onPaymentList();
    },
    handleCurrentChange(val) {
      this.pageIndex = val;
      this.onPaymentList();
    },

    //查看
    onDetails(id) {
      this.dialogForms = true;
      var dto = {
        id: id,
      };
      ReversePaymentapi(dto).then((res) => {
        this.forms = res.data.data;
      });
    },

    //弹框
    onAdd() {
      this.dialogFormVisible = true;
      //自动生成产品编号
      this.form.serialNumber = autoGenerateCode("YF");
    },

    //添加应收信息
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          AddPaymentapi(this.form).then(
            (res) => {
              if (res.data.totalCount > 0) {
                this.$message({
                  message: "添加成功",
                  type: "success",
                });
              }
              this.dialogFormVisible = false;
            },
            (err) => {
              this.$message({
                message: "添加失败",
                type: "error",
              });
            }
          );
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.dialogFormVisible = false;
    },

    //查看应收信息
    CheckView(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.dialogForms = false;
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    returnForms(formName) {
      this.dialogForms = false;
    },

    //删除信息
    onDelete(id) {
      this.$confirm("此操作将永久删除信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          var dto = {
            id: id,
          };
          DeletePaymentapi(dto).then((res) => {
            if (res.data.totalCount > 0) {
              this.$message.success("删除成功");
              this.onPaymentList();
            } else {
              this.$message("删除失败");
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    //批量删除
    onBathdel() {
      if (this.multipleSelection.length == 0) {
        this.$message.error("必须选择一项删除");
        return;
      }
      this.$confirm("确定要批量删除应收信息吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          var ids = [];
          this.multipleSelection.forEach((element) => {
            ids.push(element.id);
          });
          var idobj = {
            id: ids.toString(),
          };
          BatchDelPaymentapi(idobj).then((res) => {
            if (res.data.totalCount > 0) {
              this.$message.success("批量删除成功");
              this.onPaymentList();
            } else {
              this.$message("批量删除失败");
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    //修改状态
    onUpdState(id) {
      var data = {
        id: id,
      };

      console.log(data.id);

      UpdateStateapi(data).then(
        (res) => {
          if (res.data.totalCount > 0) {
            this.$message.success("审核成功");
          }
          this.onPaymentList();
        },
        (err) => {
          this.$message.error("审核失败");
          console.error(err);
        }
      );
    },

    //跳转
    onjump(id) {
      this.$router.push("PaymentUpd?id=" + id);
    },
  },
  created() {
    //加载应付列表
    this.onPaymentList();

    //加载支付类别
    this.onTypeList();
  },
};
</script>

<style>
.text-left {
  text-align: left;
}
</style>